function send() {
	hide_all();
	clean_all();
	
	var isEugene = document.getElementById(".isEugene").checked;
	var qubNum = parseInt(document.getElementById("qub_num").value + document.getElementById("qub_num1").value);
	
	var encodrs = [];

	var keys = generateKeys(qubNum, isEugene, encodrs);
	var keyA = keys["keyA"];
	var keyB = keys["keyB"];

	var overlaps = BB84.compareKeys(keyA, keyB);
	var qubit_eff = parseFloat(keyA.length/qubNum*100).toFixed(2);
	
	$(".qubitEff").text(" "+qubit_eff+"%");
	
	if (overlaps < 1) $(".overlap").css('color','red');
	else $(".overlap").css('color','black');
	
	$(".overlap").text("Key overlap: " + parseFloat(overlaps*100.0).toFixed(2) + "%");
	
	$('body').animate({scrollTop: $(document).height()}, 'slow');
}

function generateKeys(length, isEugene, alt_encoders) {
	var keyA = [];
	var keyB = [];
	var cur_length = 0;

	if (length < 1) {
		alert("[ERR] Pass transmitted string!");
		return {"keyA": keyA, "keyB": keyB};
	}
	
	else {
		keys = generateKeysRoutine(length, isEugene, alt_encoders);
		
		keyA = keyA.concat(keys["keyA"]);
		keyB = keyB.concat(keys["keyB"]);

		cur_length++;
	} 

	return {"keyA": keyA, "keyB": keyB};
}

function generateKeysRoutine(length, isEugene, alt_encoders) {
	var encoders = [
					[0, 90],
					[45, 135]
				   ];
	
	if (alt_encoders.length > 0) {
		encoders = alt_encoders;
	}
				   
	var photons = [];
//	Генерируются кубиты от Алины.
	var qubits = BB84.getQubits(length), //BB84.getQubits(length << 1);
	basiss = [],
	values = [];
	
	for (var i = 0; i < qubits.length; i++) {
		var tmp_bas = qubits[i]["basis"],
			tmp_val = qubits[i]["value"];
		
		basiss.push(tmp_bas);
		values.push(tmp_val);
		
		photons[i] = encoders[tmp_bas][tmp_val];
	}

	document.getElementById(".sideA.basis").value = bitsToString(basiss);
	document.getElementById(".sideA.value").value = bitsToString(values);
	//document.getElementById("photons").innerText = photons.join(',');
	displayPhotons(photons);
	$('#canvas .line').attr('title',photons);
	
	

	//	Евгений прослушивает квантовый канал.
	if (isEugene) {
		eubiss = BB84.getBasis(length); //BB84.getBasis(length << 1);
		eubits = BB84.mesureQubits(qubits, eubiss);

		states = eubits;
	}
	else {
		states = qubits;
	}

	//	Борис принимает и измеряет состояния кубитов в различных поляризациях.
	bobiss = BB84.getBasis(length); //BB84.getBasis(length << 1);
	bobits = BB84.mesureQubits(states, bobiss);

	basiss = [];
	values = [];
	
	for (var i = 0; i < bobits.length; i++) {
		basiss.push(bobits[i]["basis"]);
		values.push(bobits[i]["value"]);
	}

	document.getElementById(".sideB.basis").value = bitsToString(basiss);
	document.getElementById(".sideB.value").value = bitsToString(values);

	// show the generated basis's and values 
	$('.result1').css('display', 'block');
	
	//	Алла и Богдан сравнивают выборы поляризаций.
	indexes = BB84.compareBasis(qubits, bobits);

	//	Аркадий и Борислава вырабатывают ключ на основе совпадений.
	keyA = BB84.getSideKey(qubits, indexes);
	keyB = BB84.getSideKey(bobits, indexes);
	
	document.getElementById(".sideA.key").value = bitsToString(keyA);
	document.getElementById(".sideB.key").value = bitsToString(keyB);
	
	// show the generated keys 
	$('.result2').css('display', 'block');

	return {"keyA": keyA, "keyB": keyB};
}

function encryptMessage () {
	var bits = [];
	var message = document.getElementById(".text").value;
	var bytes = toBytes(message);
	
	for (var i = 0; i < bytes.length; i++) {
		bits = bits.concat(toBits(bytes[i]));
	}
		
	document.getElementById(".bits").value = bitsToString(bits);
}

function encryptMessage() {
	var message = document.getElementById(".text").value,
		key		= document.getElementById(".sideA.key").value,
		res		= "";
		
	var bits = [];
	var bytes = toBytes(message);

	for (var i = 0; i < bytes.length; i++) {
		bits = bits.concat(toBits(bytes[i]));
	}
	
	res = BB84.encrypt(bits, key);
	
	$('#encMessage').text(res);
	$('.result4').css("display", "block");
	
	$('body').animate({scrollTop: $(document).height()}, 'slow');
		
	return 0;
}

function step2() {
	$('.result3').css("display","block");
	$('body').animate({scrollTop: $(document).height()}, 'slow');
}

function display_protocol_constr() {
	hide_all();
	clean_all();
	
	$('.protocol_construction').css('display','block');
}

function display_basis_angle(num) {
	var basiss = ['alice','bob'];
	
	$('#'+basiss[num]+'_basis_angle').css('display','block');
}

function new_protocol() {
	var basisA = [],
		basisB = [];
	
	basisA[0] = parseInt(document.getElementById('alice_basis_num').value);
	basisA[1] = parseInt(document.getElementById('alice_basis_angle').value);
	
	basisB[0] = parseInt(document.getElementById('bob_basis_num').value);
	basisB[1] = parseInt(document.getElementById('bob_basis_angle').value);
	
	//if ((basisA[0] < 1) or (basisB[0] < 1)) alert("Please, correct the number of basis vectors");
	
	var isEugene = document.getElementById(".isEugene").checked;
	var qubNum = document.getElementById("qub_num").value;
	
	var encoders = new Array();
		
	encoders[0] = [0,0];
	encoders[1] = [0,0];
		
	for (var j = 0; j < 2; j++) {
		
		for (var i = 0; i < basisA[0]; i++) {
		
			encoders[0][i] = basisA[1]+i*90;
			encoders[1][i] = basisB[1]+i*90;
		}
	}


	var keys = generateKeys(qubNum, isEugene, encoders);
	var keyA = keys["keyA"];
	var keyB = keys["keyB"];

	var overlaps = BB84.compareKeys(keyA, keyB);
	var qubit_eff = parseFloat(keyA.length/qubNum*100).toFixed(2);
	
	$(".qubitEff").text(" "+qubit_eff+"%");
	 
}
